﻿using EntityLibrary.DomainCheck;
using System;
using System.Linq;
using System.Xml;

namespace EntityLibrary.Utils
{
    public static class XMLDocumentExtension
    {
        /// <summary>
        /// 判断入参是否非法
        /// </summary>
        /// <param name="xml">扩展XmlDocument的方法</param>
        /// <param name="path">xml的路径描述</param>
        /// <param name="allowEmpty">是否允许为空</param>
        /// <param name="value">返回值</param>
        /// <param name="type">判断时间类型</param>
        /// <param name="s">判断是否在限定的类型内</param>
        /// <param name="fieldType">根据枚举类给定的类型判断是否合规 IDCardNo:身份证,Tel电话号码,Date判断时间类型</param>
        public static void Check(this XmlDocument xml, string path, bool allowEmpty, out object value, FieldType fieldType = FieldType.String, string[] s = null)
        {

            var errorMessage = string.Empty;
            XmlNode Node = null;
            string valueStr = string.Empty;
            string NodeName = string.Empty;

            try
            {
                Node = xml.SelectSingleNode(path);
                valueStr = Node.InnerText.Trim();
                NodeName = Node.Name;
            }
            catch (Exception)
            {
                if (!allowEmpty) throw new InvalidOperationException("未找到相应的节点" + path);
            }

            value = valueStr;
            //判断是否允许为空
            if (!allowEmpty && string.IsNullOrWhiteSpace(valueStr))
            {
                errorMessage = NodeName + "不能为空！";
                throw new InvalidOperationException(errorMessage);
            }

            if (fieldType == FieldType.Date)
            {
                //判断日期格式是否正确
                if (!DateTime.TryParse(valueStr, out var valueDateTime) && !allowEmpty)
                {
                    errorMessage = NodeName + "格式错误！";
                    throw new InvalidOperationException(errorMessage);
                }
                else value = valueDateTime;
            }
            else if (fieldType == FieldType.Integer)
            {
                if (!int.TryParse(valueStr, out var valueInteger) && !allowEmpty)
                {
                    errorMessage = NodeName + "格式错误！";
                    throw new InvalidOperationException(errorMessage);
                }
                else value = valueInteger;
            }
            else if (fieldType == FieldType.Money)
            {
                if (!decimal.TryParse(valueStr, out var valueMoney) && !allowEmpty)
                {
                    errorMessage = NodeName + "格式错误！";
                    throw new InvalidOperationException(errorMessage);
                }
                else value = valueMoney;
            }
            //文档中代码字典中限定范围值的有效性判断
            if (s != null && s.Length > 0 && s.SingleOrDefault(c => c.Equals(valueStr)) == null && !allowEmpty)
            {
                errorMessage = NodeName + "不符合规范！";
                throw new InvalidOperationException(errorMessage);
            }
        }

    }
}
